/*
 * 你好
 *	ComboBox
 *	By Jared Nuzzolillo
 *
 *	Updated by Erik Arvidsson
 *	http://webfx.eae.net/contact.html#erik
 *	2002-06-13	Fixed Mozilla support and improved build performance
 *
 */

	function ExtraSelect(xSelectId, data) {
		this.xSelectId = xSelectId;
		this.data = data;

		this.obj = document.getElementById(xSelectId);
		this.objDesc = document.getElementById(xSelectId+"_desc");
		//this.objDisplay = document.getElementById(xSelectId+"_display");
		this.objSelect = document.getElementById(xSelectId+"_select");
		this.objBack = document.getElementById(xSelectId+"_back");
		this.children = this.objBack.getElementsByTagName("op");

		this.xSelectSet = xSelectSet;
		this.xSelectUnload = xSelectUnload;
		this.xSelectLoad = xSelectLoad;
		this.xSelectFind = xSelectFind;
		this.xSelectInit = xSelectInit;
		this.xMoveSelect = xMoveSelect;
		this.xSelectDependOnInit = xSelectDependOnInit;
		this.extraLoaded = extraLoaded;
		this.fetchExtraSelectData = fetchExtraSelectData;
		this.clearExtraOption = clearExtraOption;
		this.addExtraOption = addExtraOption;

		this.xSelectInit();
	}

	function xSelectInit() {
		if (this.data && this.data != '') {
			for (var i = 0;i < this.children.length; i ++) {			
				if (this.children[i].getAttribute("value") == this.data) {
					this.obj.value = this.data;
					this.objDesc.value = this.children[i].getAttribute("text");
					break;
				}
			}
		}
	}
	
	function xSelectSet() {
		if (this.objSelect.value != "") {
			this.obj.value = this.objSelect.value;
			for (var i = 0;i < this.objSelect.options.length; i ++) {
				if (this.objSelect.options.item(i).value == this.objSelect.value) {
					this.objDesc.value = this.objSelect.options.item(i).text;
					break;
				}					
			}
		}
		//this.xSelectUnload();
	}	

	function xSelectUnload() {		
		this.objSelect.style.display = "none";
		if (this.obj.value == "")
			this.objDesc.value = "";
	}
	
	function xSelectLoad() {
		var sLeft = 0;
		var sTop = 0;
		
		if (document.getElementsByName("body").length != 0) {
			var sLeft = document.body.scrollLeft;
			var sTop = document.body.scrollTop;
		}
		
		var dTop = getBoundingClientBottom(this.objDesc) + sTop;
		var dLeft = getBoundingClientLeft(this.objDesc) + sLeft;

		this.objSelect.style.left = dLeft;
		this.objSelect.style.top = dTop;
		this.objSelect.style.display = "block";
		var optionsObj = this.objSelect.options;
		for (var k = optionsObj.length - 1; k >= 0 ; k --) {
			this.objSelect.removeChild(optionsObj.item(k));
		}

		
		if (this.obj.value != "") {
			for (var i = 0;i < this.children.length; i ++) {			
				if (this.children[i].getAttribute("value") == this.obj.value) {
					var oOption = document.createElement("OPTION");	
					oOption.text = this.children[i].getAttribute("text");
					oOption.value = this.children[i].getAttribute("value");
					if (document.all)
						this.objSelect.add(oOption);
					else
						this.objSelect.appendChild(oOption);
					break;
				}
			}
		} else {
			for (var i = 0;i < this.children.length; i ++) {			
				var oOption = document.createElement("OPTION");	
				oOption.text = this.children[i].getAttribute("text");
				oOption.value = this.children[i].getAttribute("value");	
				if (document.all)
					this.objSelect.add(oOption);
				else
					this.objSelect.appendChild(oOption);				
			}
		}
	}
	
	function xSelectFind(event) {
		if (event.keyCode == 13) {
			//this.xSelectSet();
			//this.xSelectUnload();
			this.objDesc.blur();
		} else if (event.keyCode == 38) {
			this.xMoveSelect(true);
		} else if (event.keyCode == 40) {
			this.xMoveSelect(false);
		} else {		
			var optionsObj = this.objSelect.options;
			for (var k = optionsObj.length - 1; k >= 0 ; k --) {
				this.objSelect.removeChild(optionsObj.item(k));
			}
		
			index = 0;
			for (var i = 0;i < this.children.length; i ++) {			
				if (startWith(this.children[i].getAttribute("text"), this.objDesc.value)) {
					var oOption = document.createElement("OPTION");	
					oOption.text = this.children[i].getAttribute("text");
					oOption.value = this.children[i].getAttribute("value");	
					if (document.all)
						this.objSelect.add(oOption);
					else
						this.objSelect.appendChild(oOption);
					index++;
				}
			}
		
			if (index == 1) {
				//event.returnValue = false;
				//oV.value = v;
				//o.value = t;
			} else {
				this.obj.value = "";
			}
		}
	}

	function xMoveSelect(isUp) {
		if (this.objSelect.options.length == 0) return;
		var j = 0;
		for (j = 0;j < this.objSelect.options.length; j ++) {
			if (this.objSelect.options.item(j).selected) {
				this.objSelect.options.item(j).selected = false;
				var x;
				if (j == 0 && isUp) {
					x = this.objSelect.options.length - 1;
					this.objSelect.options.item(this.objSelect.options.length - 1).selected = true;
				} else if (j == this.objSelect.options.length - 1 && !isUp) {
					x = 0;
					this.objSelect.options.item(0).selected = true;
				} else if (isUp) {
					x = j - 1;
					this.objSelect.options.item(j-1).selected = true;
				} else if (!isUp) {
					x = j + 1;
					this.objSelect.options.item(j+1).selected = true;
				}
				this.obj.value = this.objSelect.value;
				this.objDesc.value = this.objSelect.options.item(x).text;
				break;
			}
		}

		if (j == this.objSelect.options.length) {
			this.objSelect.options.item(0).selected = true;
			this.obj.value = this.objSelect.value;
			this.objDesc.value = this.objSelect.options.item(0).text;
		}
	}
	
	function fetchExtraSelectData(param) {
		frmObj = document.getElementsByName("iFrameExtraSelect_" + this.xSelectId)[0];
		src = frmObj.getAttribute("url");
		if (param)
			src += "&" + param;
		frmObj.src = src;
	}
	
	function extraLoaded() {			
		if (typeof this.onload == "function") {
			this.onload();
		}
	}
	
	function xSelectDependOnInit(dependOnId, weakDepend, dependParam) {
		var obj = this;
		
		eval("var " + obj.xSelectId + "PreChangeFun = function() {}");
		if (document.getElementById(dependOnId).onchange) {
			eval(obj.xSelectId + "PreChangeFun = document.getElementById(dependOnId).onchange;");
		}
		

		document.getElementById(dependOnId).onchange = function (dependObj) {
			if (!dependObj || dependObj == '[object Event]') dependObj = this;
			eval(obj.xSelectId + "PreChangeFun(dependObj);");

			if (dependObj.value && dependObj.value != "") {
				if (dependParam)
					obj.fetchExtraSelectData(dependParam+"=" + dependObj.value);
				else
					obj.fetchExtraSelectData(dependObj.name+"=" + dependObj.value);				
			} else {
				if (weakDepend)
					obj.fetchExtraSelectData();
				else
					obj.clearExtraOption();
			}
				
		}

		document.getElementById(dependOnId).onchange();
	}
	
	function clearExtraOption() {
		for (var i = this.children.length - 1; i >= 0 ; i --) {
			this.objBack.removeChild(this.children[i]);
		}
		this.children = this.objBack.getElementsByTagName("op");
		this.obj.value = "";
		this.objDesc.value = "";
	}
	
	function addExtraOption(value, text) {
		var opObj = document.createElement("op");
		opObj.setAttribute("value", value);
		opObj.setAttribute("text", text);
		this.objBack.appendChild(opObj);
		this.children = this.objBack.getElementsByTagName("op");
	}